<?php defined('INDEX_FILE') or die('error001');


/**
* MLT Web Engine
* http://www.molot-soft.ru
*
* @package MLT_WE
* @subpackage Simple_TPL
* @author Ivan Chistyakov <ivan@molot-soft.ru>
* @version $Id: tree.stpl.php 40 2008-03-18 11:18:05Z molotsoft@gmail.com $
* @link $HeadURL: http://simple-tpl.googlecode.com/svn/trunk/tags/tree.stpl.php $
*/


class stpl_tree_element extends simple_tpl_element {

	private $ids = array();
	private $parent_ids = array();

	private $rows = array();

	final public function get_rows() {
		return $this->rows;
	}

	final public function get_num_rows() {
		return count(self::get_rows());
	}

	final public function has_rows() {
		return self::get_num_rows() >= 1;
	}

	final public function flush_row() {
		$i = $this->get_num_rows();
		$row['_id_'] = $i;
		$row['_odd_'] = $row['_id_'] % 2 === 1;
		$row['_even_'] = $row['_id_'] % 2 === 0;
		$this->merge_variables($row);
		if ($this->isset_variable('id') && $this->isset_variable('parent_id')) {
			$this->ids[$i] = (int)$this->get_variable('id');
			$this->parent_ids[$i] = (int)$this->get_variable('parent_id');
			/*
			if ($row['_odd_'] && $this->isset_child('body_o')) {
				$this->rows[$i] = $this->get_child('body_o')->get_data();
			} elseif ($row['_even_'] && $this->isset_child('body_e')) {
				$this->rows[$i] = $this->get_child('body_e')->get_data();
			} elseif ($this->isset_child('body')) {
				$this->rows[$i] = $this->get_child('body')->get_data();
			} else {
				$this->rows[$i] = '';
			}
			*/
			if ($this->isset_child('body')) {
				$this->rows[$i] = $this->get_child('body')->get_data();
			} else {
				$this->rows[$i] = '';
			}
		}
	}

	final public function clear_row() {
		$this->clear_variables();
	}

	final public function set_row(array $row = array()) {
		$this->set_variables($row);
	}

	final public function merge_row(array $row = array()) {
		$this->merge_variables($row);
	}

	final public function assign_row(array $row = array()) {
		$this->set_row($row);
	}

	final public function add_row(array $row = array()) {
		$this->assign_row($row);
		$this->flush_row();
	}

	final public function clear_rows() {
		$this->rows = array();
	}

	final public function set_rows(array $rows = array()) {
		$this->clear_rows();
		$this->merge_rows($rows);
	}

	final public function merge_rows(array $rows = array()) {
		foreach ($rows as $row) {
			$this->add_row($row);
		}
	}

	final public function assign_rows(array $rows = array()) {
		$this->set_rows($rows);
	}

	public function get_data($parent_id = 0) {
		$data = '';
		if ($this->has_rows()) {
			$rows = array();
			foreach ($this->get_rows() as $i => $row) {
				if (($this->ids[$i] >= 1) && ($parent_id === $this->parent_ids[$i])) {
					$rows[$i] = str_ireplace('{!node}', $this->get_data($this->ids[$i]), $row);
				}
			}
			if (count($rows) >= 1) {
				$data .= $this->get_param('head');
				$data .= implode($this->get_param('glue'), $rows);
				$data .= $this->get_param('foot');
			}
		} elseif ($this->isset_param('no_body')) {
			$data .= $this->get_param('head');
			$data .= $this->get_param('no_body');
			$data .= $this->get_param('foot');
		} else {
			$data .= $this->get_param('empty');
		}
		return $data;
	}

}


?>